package cn.yikaojiuguo.student.ui.my;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;

public class SettingActivity extends CustomerActionBarActivity implements View.OnClickListener{

    @Bind(R.id.rl_pwd)
    RelativeLayout mRl_pwd;
    @Bind(R.id.tv_number)
    TextView mTv_number;
    @Bind(R.id.rl_message)
    RelativeLayout mRl_message;
    @Bind(R.id.rl_clear)
    RelativeLayout mRl_clear;
    @Bind(R.id.tv_cache)
    TextView mTv_cache;
    @Bind(R.id.rl_score)
    RelativeLayout mRl_score;
    @Bind(R.id.btn_logout)
    Button mBtn_logout;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(),SettingActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
        initEvents();
    }

    private void initView() {
        resetBindNumber();
        mTv_cache.setText(Formatter.formatFileSize(this, Util.getFileSize(YiKaoApplication
                .getCacheFilePath())));
    }

    private void resetBindNumber() {
        String number = YiKaoApplication.getMobileNubmer();
        if(TextUtils.isEmpty(number)) {
            mTv_number.setText("未绑定");
        } else {
            mTv_number.setText(number);
        }
    }

    private void initEvents() {
        mRl_pwd.setOnClickListener(this);
        mRl_message.setOnClickListener(this);
        mRl_clear.setOnClickListener(this);
        mRl_score.setOnClickListener(this);
        mBtn_logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_pwd:
                startActivity(ChangePwdActivity.newIntent());
                break;
            case R.id.rl_message:
                startActivity(MessageSettingActivity.newIntent());
                break;
            case R.id.rl_clear:
                showClearCacheDialog();
                break;
            case R.id.rl_score:
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id" +
                            "=" + getPackageName()));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    ToastUtil.showToast(this,"你的手机未安装市场应用,暂不能进行评分");
                }
                break;
            case R.id.btn_logout:
                showProgressDialog("正在退出...");
                Util.logOut();
                break;
        }
    }

    private void showClearCacheDialog() {
        new AlertDialog.Builder(this).setTitle("清除缓存").setMessage("根据缓存文件的大小，清理时间从几秒到几分钟不等，请耐心等待。")
                .setPositiveButton("清除", new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //ToastUtil.showToast(SettingActivity.this, "缓存清理中,请稍候...");
                        Util.clearCache();
                    }
                }).setNegativeButton("取消", null).create().show();
    }

    @Override
    public void onEvent(MessageEvent event) {
        super.onEvent(event);
        if(event.messageType == MessageEvent.MessageType.REFRESH_CACHE_SIZE) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mTv_cache.setText(Formatter.formatFileSize(SettingActivity.this, Util.getFileSize(YiKaoApplication.getCacheFilePath())));
                    ToastUtil.showToast(SettingActivity.this,"清理成功");
                }
            });
        }
    }
}
